﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Hosting;
using System.Web.UI.HtmlControls;
using EPiCloner.ResourcesRegistration;
using EPiServer;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.PlugIn;

namespace EPiCloner.Templates.Plugins.Cloning
{
    [GuiPlugIn(DisplayName = "EPiCloner", Description = "Clones Site sub tree required language.", DefaultEnabled = true, Area = PlugInArea.ActionWindow, Url = "/EPiCloner.aspx")]
    public partial class CloningPlugin : SimplePage
    {
        protected void BtnSubmitClicked(object sender, EventArgs e)
        {
            if (!PageReference.IsNullOrEmpty(pageToClone.PageLink) && !PageReference.IsNullOrEmpty(newPageParent.PageLink))
            {
                if (cbDisablePageEvents == null)
                {
                    cbDisablePageEvents = new HtmlInputCheckBox();
                    cbDisablePageEvents.Checked = true;
                }

                string message;

                try
                {
                    message = Cloner.Clone(pageToClone.PageLink, newPageParent.PageLink, inputLanguage.SelectedLanguage,
                                           cloner => cloner.SetPageName(inputName.Value)
                                                         .ShouldPublish(cbShouldPublish.Checked)
                                                         .ShouldCloneDescendents(cbShouldCloneWithDescendants.Checked)
                                                         .ShouldUpdatePageReferences(cbShouldUpdarePageReferences.Checked)
                                                         .ShouldSuppressExceptions(cbShouldCloneWithDescendants.Checked)
                                                         .ShouldDisablePageEvents(cbDisablePageEvents.Checked));
                }
                catch (Exception ex)
                {
                    message = ex.ToString();
                }

                litResults.Text = message;
            }
        }
    }
}